﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class course : System.Web.UI.UserControl
{
    DBaccess db = new DBaccess();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            MyVector mv2 = new MyVector(int.Parse(Session["mid"].ToString()));
            String cname = "";
            String cid = "";
            mv2.Recommand(0.1);
            MyVector mv2reco = mv2.GetRecommand();
            mv2reco.Sort();
            mv2.Sort();
            DataSet ds = new DataSet();
            if (mv2reco.GetLength() != 0)
            {
                ds = db.GetDataSet("select * from mr_course where cname = '" + mv2reco.GetIname(0) + "'");
                cname = ds.Tables[0].Rows[0]["cname"].ToString();
                cid = ds.Tables[0].Rows[0]["cid"].ToString();
                Image1.ImageUrl = ds.Tables[0].Rows[0]["logo"].ToString();
                HyperLink1.Text = cname;
                HyperLink1.NavigateUrl = "reco_resource.aspx?cid=" + cid;
                HyperLink2.Text = "推荐度：" + mv2reco.GetValue(0).ToString("0.00");
                ImageButton2.ImageUrl = "~/Picture/previous2.png";
                ImageButton2.Enabled = false;
            }
            else
                this.Visible = false;
        }
    }

    public void GoNext(String name)
    {
        MyVector mv2 = new MyVector(int.Parse(Session["mid"].ToString()));
        ImageButton2.Enabled = true;
        ImageButton2.ImageUrl = "~/Picture/previous1.png";
        String cname = "";
        String cid = "";
        mv2.Recommand(0.1);
        MyVector mv2reco = mv2.GetRecommand();
        mv2reco.Sort();
        int i = 0, index = 0;
        for (i = 0; i < mv2reco.GetLength(); i++)
            if (mv2reco.GetIname(i) == name)
                break;
        if (i != mv2reco.GetLength() - 1) index = i + 1;
        if (index == mv2reco.GetLength() - 1)
        {
            ImageButton1.ImageUrl = "~/Picture/next2.png";
            ImageButton1.Enabled = false;
        }
        DBaccess db = new DBaccess();
        DataSet ds = new DataSet();
        ds = db.GetDataSet("select * from mr_course where cname = '" + mv2reco.GetIname(index) + "'");
        cname = ds.Tables[0].Rows[0]["cname"].ToString();
        cid = ds.Tables[0].Rows[0]["cid"].ToString();
        Image1.ImageUrl = ds.Tables[0].Rows[0]["logo"].ToString();
        HyperLink1.Text = cname;
        HyperLink1.NavigateUrl = "reco_resource.aspx?cid=" + cid;
        HyperLink2.Text = "推荐度：" + mv2reco.GetValue(index).ToString("0.00");
    }

    public void GoPrevious(String name)
    {
        MyVector mv2 = new MyVector(int.Parse(Session["mid"].ToString()));
        ImageButton1.Enabled = true;
        ImageButton1.ImageUrl = "~/Picture/next1.png";
        String cname = "";
        String cid = "";
        mv2.Recommand(0.1);
        MyVector mv2reco = mv2.GetRecommand();
        mv2reco.Sort();
        int i = 0, index = 0;
        for (i = 0; i < mv2reco.GetLength(); i++)
            if (mv2reco.GetIname(i) == name)
                break;
        if (i != 0) index = i - 1;
        if (index == 0)
        {
            ImageButton2.ImageUrl = "~/Picture/previous2.png";
            ImageButton2.Enabled = false;
        }
        DBaccess db = new DBaccess();
        DataSet ds = new DataSet();
        ds = ds = db.GetDataSet("select * from mr_course where cname = '" + mv2reco.GetIname(index) + "'");
        cname = ds.Tables[0].Rows[0]["cname"].ToString();
        cid = ds.Tables[0].Rows[0]["cid"].ToString();
        Image1.ImageUrl = ds.Tables[0].Rows[0]["logo"].ToString();
        HyperLink1.Text = cname;
        HyperLink1.NavigateUrl = "reco_resource.aspx?cid=" + cid;
        HyperLink2.Text = "推荐度：" + mv2reco.GetValue(index).ToString("0.00");
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (double.Parse(TextBox1.Text) > 5)
            Response.Write("<script>alert('评分不能超出最大值5');</script>");
        else
        {
            int sid = int.Parse(Session["mid"].ToString());
            DataSet ds = new DataSet();
            ds = db.GetDataSet("select * from mr_course where cname = '" + HyperLink1.Text + "'");
            String sql = "insert into mr_recommand values (" + Session["mid"].ToString() + "," + ds.Tables[0].Rows[0]["cid"].ToString() + "," + TextBox1.Text + ")";
            int result = db.ExecSql(sql);
            if (result == 1)
            {
                Response.Write("<script>alert('评分成功!');</script>");
                GoPrevious(HyperLink1.Text);
            }
            else
                Response.Write("<script>alert('评分失败!');</script>");
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        GoNext(HyperLink1.Text);
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        GoPrevious(HyperLink1.Text);
    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        this.Visible = false;
    }
}